package com.ia.uniweb.service;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import me.prettyprint.cassandra.serializers.ByteBufferSerializer;
import me.prettyprint.cassandra.serializers.IntegerSerializer;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.HSuperColumn;
import me.prettyprint.hector.api.beans.SuperSlice;
import me.prettyprint.hector.api.exceptions.HectorException;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.hector.api.query.QueryResult;
import me.prettyprint.hector.api.query.SuperSliceQuery;

import com.ia.uniweb.model.Result;

public class ResultServiceImpl implements ResultService {

	private Keyspace keyspace;

	private static final String SUPERCOLUMNFAMILY = "Results";

	public ResultServiceImpl(Keyspace keySpace) {
		this.keyspace = keySpace;
	}

	@Override
	public List<Result> getResultsForStudent(String regNumber) {

		SuperSliceQuery<String, String, String, ByteBuffer> squery = HFactory
				.createSuperSliceQuery(keyspace, StringSerializer.get(),
						StringSerializer.get(), StringSerializer.get(),
						ByteBufferSerializer.get());

		squery.setColumnFamily(SUPERCOLUMNFAMILY);
		squery.setKey(regNumber);
		squery.setRange("", "", false, 999);

		QueryResult<SuperSlice<String, String, ByteBuffer>> sresults = squery
				.execute();

		List<HSuperColumn<String, String, ByteBuffer>> individualResults = sresults
				.get().getSuperColumns();

		Result[] results = null;
		if (individualResults != null) {
			results = new Result[individualResults.size()];
			int i = 0;
			for (HSuperColumn<String, String, ByteBuffer> hSuperColumn : individualResults) {

				Result result = new Result();

				result.setRegistrationNumber(regNumber);
				result.setSubject(StringSerializer.get().fromByteBuffer(
						hSuperColumn.getSubColumnByName("subject").getValue()));
				result.setGrade(StringSerializer.get().fromByteBuffer(
						hSuperColumn.getSubColumnByName("grade").getValue()));
				result.setInternalMarks(IntegerSerializer.get().fromByteBuffer(
						hSuperColumn.getSubColumnByName("internalMarks")
								.getValue()));
				result.setPracticalMarks(IntegerSerializer.get()
						.fromByteBuffer(
								hSuperColumn.getSubColumnByName(
										"practicalMarks").getValue()));
				result.setTheoryMarks(IntegerSerializer.get().fromByteBuffer(
						hSuperColumn.getSubColumnByName("theoryMarks")
								.getValue()));
				result.setResult(StringSerializer.get().fromByteBuffer(
						hSuperColumn.getSubColumnByName("result").getValue()));

				System.out.println("Result item : " + result);

				results[i++] = result;
			}
		}

		return (results != null && results.length > 0) ? Arrays.asList(results)
				: null;
	}

	@Override
	public Result getResultForStudent(String regNumber, String subjectCode) {
		return null;
	}

	@Override
	public boolean uploadResultsForStudent(String regNumber,
			List<Result> results) {
		StringSerializer stringSerializer = StringSerializer.get();

		boolean uploadStatus = true;
		try {
			Mutator<String> mutator = HFactory.createMutator(keyspace,
					StringSerializer.get());

			for (Iterator iterator = results.iterator(); iterator.hasNext();) {
				Result result = (Result) iterator.next();

				mutator.insert(regNumber, SUPERCOLUMNFAMILY, HFactory
						.createSuperColumn(result.getSubjectCode(), Arrays
								.asList(HFactory.createStringColumn("subject",
										result.getSubject()), HFactory
										.createStringColumn("result",
												result.getResult()), HFactory
										.createStringColumn("grade",
												result.getGrade())),
								stringSerializer, stringSerializer,
								stringSerializer));

				mutator.insert(regNumber, SUPERCOLUMNFAMILY, HFactory
						.createSuperColumn(result.getSubjectCode(), Arrays
								.asList(HFactory.createColumn("practicalMarks",
										result.getPracticalMarks(),
										stringSerializer,
										IntegerSerializer.get()), HFactory
										.createColumn("theoryMarks",
												result.getTheoryMarks(),
												stringSerializer,
												IntegerSerializer.get()),
										HFactory.createColumn("internalMarks",
												result.getInternalMarks(),
												stringSerializer,
												IntegerSerializer.get())),
								stringSerializer, stringSerializer,
								IntegerSerializer.get()));

			}

		} catch (HectorException e) {
			e.printStackTrace();
			uploadStatus = false;
		}

		return uploadStatus;

	}

}
